﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Nette
{
    /// <summary>
    /// FileLine.xaml 的交互逻辑
    /// </summary>
    public partial class FileLine : UserControl
    {

        public event RoutedEventHandler DeleteClick;
        public event RoutedEventHandler TLostFocus;

        public static readonly int TYPE_VALUE = 0;
        public static readonly int TYPE_FILE = 1;

        public FileLine()
        {
            InitializeComponent();
            this.IKey.LostKeyboardFocus += onLostFocus;
            this.IVal.LostKeyboardFocus += onLostFocus;
            //this.IFiles.LostFocus += onLostFocus;
            this.IFiles.MouseDoubleClick += IVal_MouseDoubleClick;
            this.IDelBtn.Click += onClick;
        }
        ~FileLine()
        {
            this.IKey.LostKeyboardFocus -= onLostFocus;
            this.IVal.LostKeyboardFocus -= onLostFocus;
            //this.IFiles.LostFocus -= onLostFocus;
            this.IFiles.MouseDoubleClick -= IVal_MouseDoubleClick;
            this.IDelBtn.Click -= onClick;
        }
        public string FileStr { get => this.IFiles.Text; set => this.IFiles.Text = value; }

        public string KeyStr
        {
            get => this.IKey.Text;
            set => this.IKey.Text=value;
        }
        public string ValueStr
        {
            get => this.IVal.Text;
            set => this.IVal.Text = value;
        }

        public string[] _file = new string[0];

        private int _type = 0;
        public int FType
        {
            get => _type;
            set
            {
                _type = value;
                if (_type == TYPE_FILE)
                {
                    this.IFiles.Visibility = Visibility.Visible;
                    this.IVal.Visibility = Visibility.Collapsed;
                }
                else if (_type == TYPE_VALUE)
                {
                    this.IFiles.Visibility = Visibility.Collapsed;
                    this.IVal.Visibility = Visibility.Visible;
                }
            }
        }

        public bool? IsSelected
        {
            get => this.IEnable.IsChecked;
            set => this.IEnable.IsChecked = value;
        }
        public bool IsCanDelete
        {
            get => this.IDelBtn.IsEnabled;
            set => this.IDelBtn.IsEnabled = value;
        }

        public string[] Files
        {
            get => _file;
            set => SetFiles(value);
        }

        public FileAttr GetData() 
        {
            FileAttr fileAttr = new FileAttr();
            fileAttr.FType = this.FType;
            fileAttr.Files = this.Files;
            fileAttr.Key = this.KeyStr;
            fileAttr.Value = this.ValueStr;
            return fileAttr;
        }

        public void SetFiles(string[] filePaths)
        {
            StringBuilder filestr = new StringBuilder();
            string[] files = new string[filePaths.Length];
            for (int i = 0; i < filePaths.Length; i++)
            {
                string path = filePaths[i];
                string _path = path.Replace("\\", "/");
                files[i] = _path;
                string filename;
                int index = _path.LastIndexOf('/');
                if (index != -1)
                {
                    filename = _path.Substring(index+1);
                }
                else {
                    filename = _path;
                }
                filestr.Append(filename).Append(";");
            }
            this._file = files;
            FileStr = filestr.ToString();
        }
        private void IVal_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //选文件
            OpenFileDialog d = new OpenFileDialog();
            d.Multiselect = true;
            if (d.ShowDialog(Application.Current.MainWindow) == false) return;
            Files = d.FileNames;
            onLostFocus(sender, null);
        }

        private void IType_Selected(object sender, SelectionChangedEventArgs e)
        {
            if (IsInitialized)
            {
                this.FType = this.IType.SelectedIndex;
            }
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            this.FType = this.IType.SelectedIndex;
        }

        private void onLostFocus(object sender, RoutedEventArgs e)
        {
           if(TLostFocus!= null) TLostFocus(this, e);
        }

        private void onClick(object sender, RoutedEventArgs e)
        {
            if (DeleteClick != null) DeleteClick(this, e);
        }

      
    }
}
